﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.DAL.Statistics;
using DevExpress.XtraPrinting;
using MINERP.ComControls.Extension;
using MINERP.COM;

namespace MINERP.Statistics.Forms.ProfessionalReports
{
    public partial class FrmSalesStatistics : Form
    {
        public static FrmSalesStatistics _instance =null;
        private FrmSalesStatistics()
        {
            InitializeComponent();
        }

        public static FrmSalesStatistics GetInstance
        {
            get 
            {
                if (_instance == null)
                {
                    _instance = new FrmSalesStatistics();
                }
                return _instance;
            }
            set { _instance = value; }
        }

        private void FrmSalesStatistics_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance = null;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            int status=2;
            Cursor = Cursors.WaitCursor;
            if (dtStart.DateTime.ToString() == "0001-1-1 0:00:00")
            {
                MessageBox.Show("请输入起始时间","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            if (dtEnd.DateTime.ToString() == "0001-1-1 0:00:00")
            {
                MessageBox.Show("请输入结束时间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DateTime start = dtStart.DateTime;
            DateTime end = dtEnd.DateTime.AddDays(1).Date;
            switch (cmbCheckOutStatus.Text)
            {
                case "全部":
                    status = 2;
                    break;
                case "未付款":
                    status = 0;
                    break;
                case "已付款":
                    status = 1;
                    break;
                default :
                    status = 2;
                    break;
            } 
            SalemanAchievementDAL dal = new SalemanAchievementDAL();
            gcSalesStatitics.DataSource = dal.GetSalesProductStatistics(start, end, status).Tables[0];
            gcSalesStatitics.RefreshDataSource();
            Cursor = Cursors.Default;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            //DevGridViewExport.ExportToExcel("销售业绩统计", gvgcSalesStatitics);
            DevControlExtension.ExportToExcel(gcSalesStatitics, this, "销售业绩统计");
            //saveFileDialog1.AddExtension = true;
            //saveFileDialog1.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
            //saveFileDialog1.FilterIndex = 3;
            //saveFileDialog1.RestoreDirectory = true;
            //if(DialogResult.OK == saveFileDialog1.ShowDialog())
            //{
            //    string fileName = saveFileDialog1.FileName;
            //    if (saveFileDialog1.OpenFile() != null)
            //    {
            //        switch (saveFileDialog1.FilterIndex)
            //        {
            //            case 3:
            //                XlsExportOptions xlsOptions = new XlsExportOptions(false);
            //                gcSalesStatitics.ExportToXls(fileName, xlsOptions);
            //                break;
            //            case 4:
            //                gcSalesStatitics.ExportToHtml(fileName);
            //                break;
            //            case 2:
            //                gcSalesStatitics.ExportToPdf(fileName);
            //                break;
            //            case 1:
            //                this.gcSalesStatitics.ExportToText(fileName);
            //                break;
            //            default:
            //                break;
            //        }
            //    }
            //}
        }

        private void gvgcSalesStatitics_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
        {
            if (e.Column.FieldName=="ServiceName")
            {
                if (e.CellValue1.ToString().Equals(e.CellValue2.ToString()))
                {
                    e.Merge = true;
                }
                else
                {
                    e.Merge = false;
                    e.Handled = true;
                }                
            }
            else
            {
                e.Handled = true;
                e.Merge = false;
            }
        }

        private void FrmSalesStatistics_Load(object sender, EventArgs e)
        {
            cmbCheckOutStatus.SelectedIndex = 0;
            dtStart.DateTime = DateTime.Now.Date;
            dtEnd.DateTime  = DateTime.Now.Date;
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
